import promisePool from './Pool.js'

export default class RoleDao {

    static async insert (role) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            //开启事务
            await connection.beginTransaction()
        
            let result = await connection.query('insert into `role` (`name`, `menuName`, `menuIndex`) values (?, ?, ?)', [role.name, role.menuName, role.menuIndex])

            if (role.permission_ids) {
                let promise_arr = []
                for (const id of role.permission_ids) {
                    promise_arr.push(connection.query('insert into `role_permission` (`role_id`, `permission_id`) values (?, ?)', [result[0].insertId, id]))
                }
                await Promise.all(promise_arr)
            }
            
            //提交事务
            await connection.commit()

            return result[0].affectedRows
        } catch (error) {
            //事务回滚
            if (connection) await connection.rollback()
            return error
        } finally {
            if (connection) connection.release()
        }
          
    }

    static async delete (role) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            //开启事务
            await connection.beginTransaction()
            
            await connection.query('delete from `role_permission` where `role_id` = ?', role.id)

            await connection.query('delete from `user_role` where `role_id` = ?', role.id)

            let result = await connection.query('delete from `role` where `id` = ?', role.id)

            //提交事务
            await connection.commit()

            return result[0].affectedRows
        } catch (error) {
            //事务回滚
            if (connection) await connection.rollback()
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async update (role) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            //开启事务
            await connection.beginTransaction()

            let result = await connection.query('update `role` set `name` = ?, `menuName` = ?, `menuIndex` = ? where `id` = ?', [role.name, role.menuName, role.menuIndex, role.id])

            if (role.permission_ids) {
                await connection.query('delete from `role_permission` where `role_id` = ?', role.id)

                let promise_arr = []
                for (const id of role.permission_ids) {
                    promise_arr.push(connection.query('insert into `role_permission` (`role_id`, `permission_id`) values (?, ?)', [role.id, id]))
                }
                await Promise.all(promise_arr)
            }

            //提交事务
            await connection.commit()

            return result[0].affectedRows
        } catch (error) {
            //事务回滚
            if (connection) await connection.rollback()
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async selectById (id) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            let [role] = await connection.query('select `id`, `name`, `menuName`, `menuIndex` from `role` where `id` = ?', id)
            
            let [permission_ids] = connection.query('select `permission_id` from `role_permission` where `role_id` = ?', role.id)

            role.permission_ids = permission_ids

            return role
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async selectByName (name) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            let [[role]] = await connection.query('select `id`, `name`, `menuName`, `menuIndex` from `role` where `name` = ?', name)
            
            let [permission_ids] = await connection.query('select `permission_id` from `role_permission` where `role_id` = ?', role.id)

            role.permission_ids = permission_ids.map(obj => obj.permission_id)

            return role
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async selectByLimit (start, end) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            let [roles] = await connection.query('select `id`, `name`, `menuName`, `menuIndex` from `role` limit ?, ' + end, start)

            let promise_arr = []
            for (const role of roles) {
                promise_arr.push(connection.query('select `permission_id` from `role_permission` where `role_id` = ?', role.id))
            }
            let results = await Promise.all(promise_arr)
            for (let i = 0; i < roles.length; i++) {
                let permission_ids = []
                for (const item of results[i][0]) {
                    permission_ids.push(item.permission_id)
                }
                roles[i].permission_ids = permission_ids
            }

            return roles
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async selectCount () {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            let result = await connection.query('select count(id) as total from role')

            return result[0][0]
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }
}